# 2024 中等  考试的最大困扰度

class Solution:
    def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
        def maxChar(ch):
            left, right, sum = 0, 0, 0
            num, ret = 0, 0
            while right < len(answerKey):
                if answerKey[right] != ch:
                    if num < k:
                        num += 1
                        sum += 1
                        right += 1
                    else:
                        if answerKey[left] != ch:
                            num -= 1
                        sum -= 1
                        left += 1
                else:
                    sum += 1
                    right += 1
                ret = max(ret, sum)
            return ret
        return max(maxChar('T'), maxChar('F'))


s = Solution()
print(s.maxConsecutiveAnswers("TTFF", 2))
print(s.maxConsecutiveAnswers("TFFT", 1))
print(s.maxConsecutiveAnswers("TTFTTFTT", 1))


# class Solution:
#     def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
#         def maxConsecutiveChar(ch: str) -> int:
#             ans, left, sum = 0, 0, 0
#             for right in range(len(answerKey)):
#                 sum += answerKey[right] != ch
#                 while sum > k:
#                     sum -= answerKey[left] != ch
#                     left += 1
#                 ans = max(ans, right - left + 1)
#             return ans
#         return max(maxConsecutiveChar('T'), maxConsecutiveChar('F'))
